package com.study.cloud.utils;

import com.aliyun.openservices.aliyun.log.producer.LogProducer;
import com.aliyun.openservices.aliyun.log.producer.Producer;
import com.aliyun.openservices.aliyun.log.producer.ProducerConfig;
import com.aliyun.openservices.aliyun.log.producer.ProjectConfig;
import com.study.cloud.apilog.entity.SLSProperties;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Date 2022/5/17
 * @Author Cliff
 * desc
 */
public class ProducerUtil {

    private static volatile Producer producer = null;

    private static final ExecutorService EXECUTOR_SERVICE = Executors
            .newFixedThreadPool(Math.max(Runtime.getRuntime().availableProcessors(), 1));

    /**
     * 获取线程池
     * @return
     */
    public static ExecutorService getExecutorService(){
        return EXECUTOR_SERVICE;
    }
    /**
     * 获取生成这对象
     * @param slsProperties
     * @return
     */
    public static Producer getInstance(SLSProperties slsProperties){
        if(producer == null){
            synchronized (ProducerUtil.class){
                if(producer == null){
                    ProducerConfig producerConfig = new ProducerConfig();
                    producer = new LogProducer(producerConfig);
                    producer.putProjectConfig(buildProjectConfig(slsProperties));
                }
            }
        }
        return producer;
    }


    private static ProjectConfig buildProjectConfig(SLSProperties slsProperties) {
        return new ProjectConfig(slsProperties.getProjectName(),
                slsProperties.getEndPoint(),
                slsProperties.getAccessId(),
                slsProperties.getAccessSecret());
    }
}
